package net.myspring.middle.web;

import net.myspring.common.domain.SearchEntity;
import net.myspring.common.utils.Message;
import net.myspring.common.utils.RequestUtils;
import net.myspring.common.utils.ThreadLocalContext;
import net.myspring.middle.domain.Account;
import net.myspring.middle.service.AccountService;
import net.myspring.middle.service.DepartmentService;
import net.myspring.middle.service.RoleService;
import net.myspring.middle.validator.AccountValidator;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Created by admin on 2016/8/10.
 */
@Controller
@RequestMapping(value = "sys/account")
public class AccountController {
    @Autowired
    private AccountValidator accountValidator;
    @Autowired
    private AccountService accountService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private RoleService roleService;


    @RequestMapping(value = "list")
    @PreAuthorize("hasAnyAuthority('PERM_sys:account:list:view')")
    public String list(Model model, HttpServletRequest request) {
        SearchEntity searchEntity = RequestUtils.getSearchEntity(request, Account.class.getName());
        model.addAttribute("page", accountService.findPage(searchEntity.getPageable(), searchEntity.getParams()));
        model.addAttribute("s", searchEntity);
        return "sys/accountList";
    }

    @RequestMapping(value = "report")
    public String report(Model model) {
        model.addAttribute("accountList", accountService.findReport());
        return "sys/accountReport";
    }

    @RequestMapping(value = "form")
    @PreAuthorize("hasAnyAuthority('PERM_sys:account:list:edit')")
    public String form(Model model, Account account) {
        model.addAttribute("account", account.getId() == null ? account : accountService.findOne(account.getId()));
        addAttribute(model);
        return "sys/accountForm";
    }

    @RequestMapping(value = "save")
    @PreAuthorize("hasAnyAuthority('PERM_sys:account:list:edit')")
    public String save(Model model, Account account, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
        boolean isCreate = account.isCreate();
        accountValidator.validate(account, bindingResult);
        if (bindingResult.hasErrors()) {
            model.addAttribute("bindingResult", bindingResult);
            addAttribute(model);
            return "sys/accountForm";
        }
        accountService.save(account);
        redirectAttributes.addFlashAttribute("message", new Message("用户保存成功"));
        if (isCreate) {
            return "redirect:/sys/account/form";
        } else {
            return "redirect:/sys/account/list";
        }
    }

    @RequestMapping(value = "k3cloudForm")
    @PreAuthorize("hasAnyAuthority('PERM_sys:account:list:k3cloudedit')")
    public String k3cloudForm(Model model) {
        model.addAttribute("account", ThreadLocalContext.get().getAccount());
        addAttribute(model);
        return "sys/accountk3cloudForm";
    }


    @RequestMapping(value = "cloudSave")
    @PreAuthorize("hasAnyAuthority('PERM_sys:account:list:k3cloudedit')")
    public String cloudSave(Model model, Account account,  RedirectAttributes redirectAttributes) {
        accountService.cloudSave(account);
        redirectAttributes.addFlashAttribute("message", new Message("用户保存成功"));
            return "redirect:/login";
    }

    @RequestMapping(value = "delete")
    @PreAuthorize("hasAnyAuthority('PERM_sys:account:list:delete')")
    public String delete(Model model, Account account, RedirectAttributes redirectAttributes) {
        accountService.delete(account);
        redirectAttributes.addFlashAttribute("message", new Message("用户删除成功"));
        return "redirect:/sys/account/list";
    }

    @RequestMapping(value = "checkLoginName")
    @ResponseBody
    public boolean checkLoginName(Model model, Account account) {
        return accountService.checkLoginName(account);
    }


    @RequestMapping(value = "/removeTab", method = RequestMethod.GET)
    @ResponseBody
    public Boolean removeTab(String menuId) {
        accountService.removeTab(menuId);
        return true;
    }

    @RequestMapping(value = "/removeAllTab", method = RequestMethod.GET)
    @ResponseBody
    public Boolean removeAllTab() {
        accountService.removeAllTab();
        return true;
    }

    private void addAttribute(Model model) {
        model.addAttribute("departments", departmentService.findAll());
        model.addAttribute("roleList", roleService.findAll());
    }
}
